<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>狮子歌歌二次元责任无限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体 -->
        <el-tree
          :data="list"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delPart(data.id, data.name)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog v-if="showDialog" :title="isEdit?'编辑':'添加'" :visible.sync="showDialog">
        <addor-edit
          :cur-id="curid"
          :all-list="allList"
          :is-edit="isEdit"
          @show-no="showNo"
          @update-depart="hGetDepartments"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from './AddorEdit'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      list: [],
      allList: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      curid: '',
      isEdit: false
    }
  },
  created() {
    this.hGetDepartments()
  },
  methods: {
    async hGetDepartments() {
      const [, res] = await getDepartments()
      // console.log(err, res)
      this.allList = res.depts
      this.list = tranListToTreeData(res.depts)
    },
    delPart(id, name) {
      if (name === '传智教育') {
        alert('叫你老子来删我吧')
        return
      }
      this.$confirm('你确认要进行删除么?', '温馨提示').then(async() => {
        // 删除操作成功
        await delDepartment(id)
        // 添加提示
        this.$message.success('删除操作成功')
        // 更新数据
        this.hGetDepartments()
      }).catch(error => {
        console.log(error)
      })
    },
    hAdd(id) {
      this.curid = id
      this.showDialog = true
      this.isEdit = false
    },
    hEdit(id) {
      this.curid = id
      this.showDialog = true
      this.isEdit = true
    },
    showNo() {
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
